<?php
//产品管理控制器
	namespace Application\Admin\Controller;
	use Application\Admin\Model\Product as ProductModel;
	use Application\Admin\Model\Procat;
	use KJ\Page;
	use KJ\Upload;
	class Product extends Admin{
		
		public function index(){
								
			$cat = new Procat();
			
			$this->cat = $cat -> select();
			
			$ProductModel = new ProductModel();	
			
			$this->userKeyWord = '';		
			
			$data = $ProductModel->order('id asc')->select();
			
			foreach($data as $k => $val){
				
				$cat_id = $val['pro_class'];
				
				$cat_name = $cat->where("id = $cat_id")->field('name')->select();
				
				$cat_name = $cat_name[0]['name'];

				$data[$k]['cat_name'] = $cat_name;
			}
			
									
			$count = count($data);
			
			$page = new Page($data, $count, 10);
			
			$data = $page->pagination();
			
			$this->data = $data;
			
			$this->render();		
		
		}
		
		public function add(){
			if(IS_POST){
				$ProductModel = new ProductModel();
				if($_FILES['image']['size'] != 0){
					$upload = new Upload();
					$result = $upload -> upload('image');
					if(!empty($result['error'])){
						echo "<script>alert('上传信息有误');
						window.history.go(-1);</script>";
						exit;
					}else{
						$ProductModel -> attributes -> pro_src = $result['realPath'];
					}
				}
				$ProductModel -> attributes -> pro_name = $_POST['title'];
				$ProductModel -> attributes -> pro_class = $_POST['cat_id'];
				
				if($ProductModel -> save()){
					echo "<script>alert('插入成功');
					window.location.href='index.php?m=admin&c=product&a=index';</script>";
					exit;
				}
				
			}
			$catModel = new Procat();
			$this->cat = $catModel->select();
			$this->render('add');
			
		}		
		
		public function delete(){
			if(IS_GET){
				$id = $_GET['id'];
				$ProductModel = new ProductModel();
				$Product = $ProductModel ->  findModel($id);
				$src = $ProductModel -> field('pro_src') -> where("id = $id") -> select();
				$src = $src[0]['pro_src'];
				if($ProductModel -> delete()){
					if(is_file($src)){
						unlink($src);
					}
					echo "<script>alert('删除成功');
					window.location.href='index.php?m=admin&c=product&a=index';
					</script>";
				}else{
					echo "<script>alert('删除失败');
					window.location.href='index.php?m=admin&c=product&a=index';
					</script>";
				}
			}
		}		
		
		public function edit(){
			if(IS_GET){
				$id = $_GET['id'];
				
				$ProductModel = new ProductModel();
				
				$this->Product = $ProductModel -> find($id);
				
				$catModel = new Procat();
				
				$this->cat = $catModel->select();
				
				$this->render('edit');
			}
			if(IS_POST){
				$id = $_POST['id'];
				$ProductModel = new ProductModel();
				$Product = $ProductModel -> findModel($id);
				if($_FILES['image']['size'] != 0){
					$upload = new Upload();
					$upload -> EXTS = array('jpeg','jpg','png');
					$result = $upload -> upload('image');
					if(!empty($result['error'])){
						echo "<script>alert('上传信息有误');window.history.go(-1);</script>";
						exit;
					}else{
						$Product -> attributes -> pro_src = $result['realPath'];
					}
				}
				$Product -> attributes -> pro_name = $_POST['title'];
				$Product -> attributes -> pro_class = $_POST['cat_id'];
				if($Product -> save()){
					echo "<script>alert('编辑成功');
					window.location.href='index.php?m=admin&c=product&a=index';</script>";
					exit;
				}
			}			
		}	
		
		public function select(){

			if(IS_POST){
				$keyword = $_POST['keyword'];
				$_SESSION['userKeyWord'] = $keyword;
				$keyword = !isset($_POST['keyword']) ? '' : "and pro_name like '%$keyword%'";
				$_SESSION['cat_id'] = $_POST['cat_id'];
				$_SESSION['keyword'] = $keyword;
				$cat_id = $_POST['cat_id'];
				$userKeyWord = $_POST['keyword'];
			}
			if(IS_GET){
				$cat_id = $_SESSION['cat_id'];
				$keyword = $_SESSION['keyword'];
				$userKeyWord = $_SESSION['userKeyWord'];
			}
			$this->cat_id = $cat_id;
			
			$this->keyword = $keyword;
			
			$this->userKeyWord = $userKeyWord;
			
			$cat = new Procat();
			
			$this->cat = $cat -> select();
			
			$produc = new ProductModel();
			
			if($cat_id == 0){
				$keyword = substr($keyword, 4);

				$produc = $produc ->where("$keyword") -> order('id asc') -> select();
			}else{
				$produc = $produc -> where("pro_class = $cat_id $keyword") -> order('id asc')->select();
			}
			
			foreach($produc as $k => $val){
				
				$cat_id = $val['pro_class'];
				
				$cat_name = $cat->where("id = $cat_id")->field('name')->select();
				
				$cat_name = $cat_name[0]['name'];
				
				$produc[$k]['cat_name'] = $cat_name;
			}
			
			$count = count($produc);
		
			$page = new Page($produc, $count, 10);
			
			$data = $page->pagination();
			
			$this->data = $data;
			
			$this->render('index');
		
		}		
		
	}
?>